package com.ruoyi.yz.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.face.domain.Member;
import com.ruoyi.face.domain.QueryCondition;
import com.ruoyi.face.domain.QueryResult;
import com.ruoyi.yz.domain.YzMember;
import com.ruoyi.yz.service.IYzMemberService;
import com.ruoyi.yz.service.impl.FaceDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/conference/device")
public class YzDeviceController extends BaseController {
    private String prefix = "/conference/device";

    @Autowired
    private FaceDeviceService faceDevice;

    @Autowired
    private IYzMemberService yzMemberService;

    @RequiresPermissions("conference:member:view")
    @GetMapping()
    public String member(ModelMap mmap)
    {
        String message =  faceDevice.isLogin() ? "已登录" : "未登录";
        mmap.put("message", message);

        return prefix + "/member";
    }


    /**
     * 查询设备内人员列表
     */
    @RequiresPermissions("device:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list()
    {
        if (!faceDevice.isLogin()) {
            throw new ServiceException("设备未登录");
        }
        startPage();
        QueryCondition query = new QueryCondition();
        Page<Object> page = PageHelper.getLocalPage();
        query.setLimit(page.getPageSize());
        query.setOffset((page.getPageNum() - 1) * page.getPageSize());
        query.setQueryStr(ServletUtils.getParameter("name"));
        QueryResult<Member> result = faceDevice.queryMember(query);

        page.setTotal(result.getPaging().getTotal());

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(result.getData());
        rspData.setTotal(result.getPaging().getTotal());

        return rspData;
    }

    /**
     * 删除人员
     */
    @RequiresPermissions("conference:member:remove")
    @Log(title = "设备人员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if (!faceDevice.isLogin()) {
            throw new ServiceException("设备未登录");
        }
        return toAjax(faceDevice.deleteMemberByIds(Arrays.asList(Convert.toLongArray((ids)))));
    }

    @PutMapping("/sync/{id}")
    public @ResponseBody AjaxResult sync(@PathVariable("id") Long id) {
        if (!faceDevice.isLogin()) {
            throw new ServiceException("设备未登录");
        }
        YzMember member = yzMemberService.selectYzMemberById(id);
        faceDevice.updateMemberById(member.createDeviceMember());
        return AjaxResult.success();
    }

    @PostMapping("/syncAll")
    public @ResponseBody AjaxResult syncAll() {
        if (!faceDevice.isLogin()) {
            throw new ServiceException("设备未登录");
        }
        List<YzMember> members = yzMemberService.selectYzMemberList(null);
        faceDevice.syncAll(members);

        return AjaxResult.success("已开始同步，请稍后刷新查看");
    }

    @PostMapping("/config")
    public @ResponseBody AjaxResult config() {

        faceDevice.updateDeviceConfig();

        return AjaxResult.success("已更新配置");
    }

    @PostMapping("/login")
    public @ResponseBody AjaxResult login() {

        faceDevice.doLogin();

        return AjaxResult.success("登录成功");
    }
}
